package shangcheng.service.impl;

import shangcheng.common.Constants;
import shangcheng.common.ServiceResultEnum;
import shangcheng.controller.vo.ShoppingCartItemVO;
import shangcheng.dao.GoodsMapper;
import shangcheng.dao.ShoppingCartMapper;
import shangcheng.entity.Goods;
import shangcheng.entity.ShoppingCart;
import shangcheng.service.ShoppingCartService;
import shangcheng.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    //todo 修改session中购物项数量

    @Override
    public String saveNewBeeMallCartItem(ShoppingCart shoppingCart) {
        ShoppingCart temp = shoppingCartMapper.selectByUserIdAndGoodsId(shoppingCart.getUserId(), shoppingCart.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            //todo count = tempCount + 1
            temp.setGoodsCount(shoppingCart.getGoodsCount());
            return updateNewBeeMallCartItem(temp);
        }
        Goods goods = goodsMapper.selectByPrimaryKey(shoppingCart.getGoodsId());
        //商品为空
        if (goods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = shoppingCartMapper.selectCountByUserId(shoppingCart.getUserId());
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (shoppingCartMapper.insertSelective(shoppingCart) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateNewBeeMallCartItem(ShoppingCart shoppingCart) {
        ShoppingCart shoppingCartUpdate = shoppingCartMapper.selectByPrimaryKey(shoppingCart.getCartItemId());
        if (shoppingCartUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出最大数量
        if (shoppingCart.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //todo 数量相同不会进行修改
        //todo userId不同不能修改
        shoppingCartUpdate.setGoodsCount(shoppingCart.getGoodsCount());
        shoppingCartUpdate.setUpdateTime(new Date());
        //修改记录
        if (shoppingCartMapper.updateByPrimaryKeySelective(shoppingCartUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public ShoppingCart getNewBeeMallCartItemById(Long newBeeMallShoppingCartItemId) {
        return shoppingCartMapper.selectByPrimaryKey(newBeeMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long newBeeMallShoppingCartItemId) {
        //todo userId不同不能删除
        return shoppingCartMapper.deleteByPrimaryKey(newBeeMallShoppingCartItemId) > 0;
    }

    @Override
    public List<ShoppingCartItemVO> getMyShoppingCartItems(Long newBeeMallUserId) {
        List<ShoppingCartItemVO> shoppingCartItemVOS = new ArrayList<>();
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(newBeeMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(shoppingCarts)) {
            //查询商品信息并做数据转换
            List<Long> newBeeMallGoodsIds = shoppingCarts.stream().map(ShoppingCart::getGoodsId).collect(Collectors.toList());
            List<Goods> goods = goodsMapper.selectByPrimaryKeys(newBeeMallGoodsIds);
            Map<Long, Goods> newBeeMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(goods)) {
                newBeeMallGoodsMap = goods.stream().collect(Collectors.toMap(Goods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (ShoppingCart shoppingCart : shoppingCarts) {
                ShoppingCartItemVO shoppingCartItemVO = new ShoppingCartItemVO();
                BeanUtil.copyProperties(shoppingCart, shoppingCartItemVO);
                if (newBeeMallGoodsMap.containsKey(shoppingCart.getGoodsId())) {
                    Goods goodsTemp = newBeeMallGoodsMap.get(shoppingCart.getGoodsId());
                    shoppingCartItemVO.setGoodsCoverImg(goodsTemp.getGoodsCoverImg());
                    String goodsName = goodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    shoppingCartItemVO.setGoodsName(goodsName);
                    shoppingCartItemVO.setSellingPrice(goodsTemp.getSellingPrice());
                    shoppingCartItemVOS.add(shoppingCartItemVO);
                }
            }
        }
        return shoppingCartItemVOS;
    }
}
